<?php
namespace frontend\controllers\wjy;


use logic\wjy\User;
use yii\web\Controller;
use yii\web\Request;

class LoginController extends Controller
{
    /**
     * 登录
     * @param Request $request
     * @return false|string
     */
    public function actionLogin(Request $request){
        try {
            $prams=$request->post();
//            print_r($prams['password']);
//            die();
            $username = User::find()->where(['username'=>$prams['username']])->asArray()->all();
//        print_r($username);
//        die();
            if(!$username){
                throw new \Exception('用户名错误');
            }
            $password=$username[0]['password'];
//            print_r($password);
//            die();
            if($prams['password']!=$password){
                throw new \Exception('密码错误');
            }
            return json_encode([
                'error_code'=>0,
                'data'=>null,
                'msg'=>'登录成功'
            ]);
        }catch (\Exception $e){
            return json_encode([
                'error_code'=>1,
                'data'=>null,
                'msg'=>$e->getMessage()
            ]);
        }

    }

    /**
     * 注册
     */
    public function actionLogon(Request $request){
//        echo 123;
//        die();
        try {
            $prams=$request->post();
            $user=new User;
            $user->username=$prams['username'];
            $user->password=$prams['password'];
            $res=$user->save();
            if($res){
                return json_encode([
                    'error_code'=>0,
                    'data'=>null,
                    'msg'=>'注册成功'
                ]);
            }
        }catch (\Exception $e){
            return json_encode([
                'error_code'=>1,
                'data'=>null,
                'msg'=>$e->getMessage()
            ]);
        }
    }

}